package com.breakfast.fun.ui.user.recharge.rechargemoney;

import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.user.User;
import com.breakfast.fun.data.entity.user.recharge.RechargeMoney;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/8/24.
 */
public class RechargeMoneyPresenter extends RechargeMoneyContract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    public void next(String price) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("orderType", "0");
        map.put("money", price);

        showLoadingDialog(true);
        rxManager.add(model.getRechargeMoneyModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<RechargeMoney>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        T.show("获取数据失败");
                        hideLoadingDialog();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(RechargeMoney rechargeMoney) {
                        if (rechargeMoney.isSuccess()) {
                            view.showPayWay(rechargeMoney);
                        } else {
                            T.show(rechargeMoney.getMsg());
                        }
                        hideLoadingDialog();
                    }


                }));
    }

    @Override
    public void getUser() {
        if (Constant.getUserData() != null) {
            Map<String, String> map = Api.getInstance().getBaseParams();

            map.put("action", "info");
            map.put("id", Constant.getUserData().getId() + "");

            rxManager.add(model.getUserModel(map)
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .unsubscribeOn(Schedulers.io())
                    .subscribe(new Subscriber<User>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            hideLoadingDialog();
                        }

                        @Override
                        public void onNext(User user) {
                            hideLoadingDialog();
                            view.refresh(user);
                        }

                    }));
        }
    }
}
